package com.etc.controller;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.MD5Utile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/UserController")
public class UserController {
    @Autowired
    UserService service;

    /**
     * 前端登录
     */
    @RequestMapping("/login")
    public ModelAndView login(@RequestParam(value = "userName", defaultValue = "") String userName,
                              @RequestParam(value = "userPwd", defaultValue = "") String userPwd,
                              HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView();
        User user = service.login(userName);
        System.out.println(user);
        if (user != null) {
            boolean b = MD5Utile.DecodeByMD5(userPwd, user.getPassword());
            if (b) {
                request.getSession().setAttribute("frontUser", user);
                request.getRequestDispatcher("/front/index.jsp").forward(request, response);
                /*response.sendRedirect("/front/index.jsp");*/
                /*mv.setViewName("redirect:/front/index.jsp");*/

                return null;
            } else {
                mv.setViewName("login");
                return mv;
            }
        } else {
            mv.setViewName("login");
            return mv;
        }
    }

    /**
     * 注册
     */
    @RequestMapping("/regist")
    public ModelAndView regist(@RequestParam(value = "userName", defaultValue = "") String userName,
                               @RequestParam(value = "password", defaultValue = "") String password,
                               HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView();
        boolean b = service.register(new User(userName, password));
        if (b) {
            mv.setViewName("login");
        } else {
            mv.setViewName("/front/register.jsp");
        }
        return mv;
    }

    /**
     * 管理员登录
     */
    @RequestMapping("/backLogin")
    public void backLogin(@RequestParam(value = "userName", defaultValue = "") String userName,
                          @RequestParam(value = "userPwd", defaultValue = "") String userPwd,
                          HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView();
        User user = service.login(userName);
        if (user != null) {
            boolean b = MD5Utile.DecodeByMD5(userPwd, user.getPassword());
            if (b && user.getUserLevel() == 1) {
                request.getSession().setAttribute("user", user);
                /*request.getRequestDispatcher("/manage/index.jsp").forward(request, response);*/
                response.sendRedirect("../manage/index.jsp");
            } else {
                request.getRequestDispatcher("/manage/login.jsp").forward(request, response);
            }
        } else {
            request.getRequestDispatcher("/manage/login.jsp").forward(request, response);
        }
    }

    /**
     * 用户修改个人信息
     */
    @RequestMapping("/updateInfo")
    public User updateInfo(User user) {
        boolean b = service.updateInfo(user, user.getUsername());
        return user;
    }

    /**
     * 后台管理员操作
     * 查询全部用户
     */
    @RequestMapping("/getAll")
    public List<User> getAll() {
        List<User> list = service.getAll();
        return list;
    }

    /**
     * 管理员修改用户信息
     */
    @RequestMapping("/updateUser")
    public boolean updateUser(User user) {
        boolean b = service.updateUser(user, user.getUsername());
        return b;
    }

    /**
     * 改变用户状态
     */
    @RequestMapping("/deleUser")
    public boolean deleUser(@RequestParam("id") Long userId, @RequestParam("status") String status) {
        boolean b = service.updatUserStatus(userId, status);
        return b;
    }

    /**
     * 增加管理员
     */
    @RequestMapping("/addUser")
    public boolean addUser(User user) {
        boolean b = service.addUser(user);
        return b;
    }

    /**
     * 根据用户等级查询用户
     */
    @RequestMapping("/getUser")
    public List<User> getUser(@RequestParam("userLevel") Integer userLevel) {
        List<User> list = service.selectUserByStatus(userLevel);
        return list;
    }

}
